package component;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Prism.core.AbstractImplementation;
import Prism.core.PrismConstants;


public class BillingSystemUI extends AbstractImplementation implements ActionListener {

	// Declare GUI AWT frame
	Frame frame;

	
	// About TOP info GUI
	Panel topPanel;
	
	// About Phone GUI
	Panel phonePanel;
	TextField phoneInput;
	Button submitPhoneButton;
	
	// About Time GUI
	Panel timePanel;
	TextField timeInput;
	Button submitTimeButton;
	
	// About Result GUI
	Panel resultPanel;
	TextArea resultTextArea;
	
	
	// About Administrative GUI
	Panel adminPanel;
	Button closeButton;
	
	@Override
	public void start() {
		super.start();
		
		// initialize GUI
		initGUI();
	}
	
	@Override
	public void handle(Prism.core.Event e) {
		if (e.getName().equals(BillingSystem.provShowBill)) {
			String result = (String) e.getParameter("result");
			showBillReplyHandle(result);
		}
	}
	// All request reply( reply of req is private, because this functions are called by only handle() and not by other outside instance
	// This is handle of showBill's request reply
	private void showBillReplyHandle(String result) {
		resultTextArea.setText(result);
	}

	// this function creates GUI
	// this is private because creating GUI is inside job of this instance
	// In other words, this is initializing, other instances is free to initialize
	private void initGUI() {
		frame = new Frame("Billing UI");
		
		// Top Info GUI
		topPanel = new Panel();
		topPanel.setLayout(new GridLayout(3,1));
		topPanel.add(new Label("Billing GUI"));
		topPanel.add(new Label("1779-1469-26"));
		topPanel.add(new Label("Kwan Park"));
		
		// retrive bill by phonenumber GUI
		phonePanel = new Panel();
		phonePanel.setLayout(new FlowLayout());
		phonePanel.add(new Label("Put Phonenumber"));
		phoneInput = new TextField(10);
		phoneInput.setText("2132548799");
		phonePanel.add(phoneInput);
		submitPhoneButton = new Button("Submit PhoneNo");
		phonePanel.add(submitPhoneButton);
		
		// retrive bills by timeperiod GUI
		timePanel = new Panel();
		timePanel.setLayout(new FlowLayout());
		timePanel.add(new Label("Start Time(Timeperiod) until Now to print bill"));
		timeInput = new TextField(10);
		timePanel.add(timeInput);
		submitTimeButton = new Button("Submit Time");
		timePanel.add(submitTimeButton);
		
		// result panel
		resultPanel = new Panel();
		resultPanel.setLayout(new FlowLayout());
		resultTextArea = new TextArea(3,60);
		resultTextArea.setEditable(false);
		resultPanel.add(resultTextArea);
		
		// Administrative GUI
		adminPanel = new Panel();
		adminPanel.setLayout(new FlowLayout());
		closeButton = new Button("close");
		adminPanel.add(closeButton);
		
		// Register Listener
		submitPhoneButton.addActionListener(this);
		submitTimeButton.addActionListener(this);
		closeButton.addActionListener(this);
		
		frame.setLayout(new GridLayout(5,1));
		frame.add(topPanel);
		frame.add(phonePanel);
		frame.add(timePanel);
		frame.add(resultPanel);
		frame.add(adminPanel);
		frame.setVisible(true);
		frame.pack();		
	}

	
	public void actionPerformed(ActionEvent e) {
		// Action handling of Close button, close Billing System UI
		if (e.getSource() == closeButton) {
			System.out.println("Closing...");
			frame.setVisible(false); // hide the Frame
			frame.dispose(); // free the system resources
			System.exit(0); // close the application
		}
		// Action handling of show offlineRecordType
		else if (e.getSource() == submitPhoneButton) {
			Prism.core.Event r = new Prism.core.Event(BillingSystem.provShowBill, PrismConstants.REQUEST);
			r.addParameter("phoneNumber", phoneInput.getText().trim());
			this.send(r);
		}
		else if (e.getSource() == submitTimeButton) {
			Prism.core.Event r = new Prism.core.Event(BillingSystem.provPrintNewBills, PrismConstants.REQUEST);
			r.addParameter("timePeriod", timeInput.getText().trim());
			this.send(r);
		}
		
	}
	
}
